let { api } = require("./../../utils/api.js");
Page({
  data: {
    avatarUrl: "",
    nickName: ""
  },
  // 判断是否授权
  getAuthorize: function() {
    let that = this;
    wx.login({
      success: function(res) {
        let code = res.code;
        if (code) {
          // 查看是否授权
          wx.getSetting({
            success: function(res) {
              if (res.authSetting["scope.userInfo"]) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                wx.getUserInfo({
                  success: function(res) {
                    that.setData({
                      avatarUrl: res.userInfo.avatarUrl,
                      nickName: res.userInfo.nickName
                    });
                    wx.request({
                      url: api + "wxLogin.do",
                      method: "POST",
                      data: {
                        code,
                        encryptedData: res.encryptedData,
                        iv: res.iv
                      },
                      success: function(res) {
                        // console.log(res.data.data);
                        wx.setStorageSync("userInfo", res.data.data);
                      }
                    });
                  }
                });
              } else {
                console.log("未授权获取用户信息");
              }
            }
          });
        } else {
          console.log("登录失败！" + res.errMsg);
        }
      }
    });
  },
  onLoad: function() {
    // this.getAuthorize();
  },
  onShow: function() {
    // 获取用户信息数据
    this.getAuthorize();
  },
  onGetUserInfo: function(e) {
    this.getAuthorize();
  },
  //进入我的评价页面
  goMyEvaluation: function() {
    if (wx.getStorageSync("userInfo").userId) {
      wx.navigateTo({
        url: "./../myEvaluation/myEvaluation"
      });
    } else {
      wx.showToast({
        title: "hi,请先登录哟 !",
        icon: "none"
      });
    }
  },
  // 进入历史菜单页面
  goHistoryMenu: function() {
    wx.navigateTo({
      url: "./../historyMenu/historyMenu"
    });
  },
  goFeedback: function() {
    wx.navigateTo({
      url: "./../feedback/feedback"
    });
  },
  adminLogin: function() {
    if (wx.getStorageSync("userInfo").userId) {
      let isLogin = wx.getStorageSync("isLogin");
      wx.navigateTo({
        url: isLogin ? "../admin/admin" : "./../login/login"
      });
    } else {
      wx.showToast({
        title: "hi,请先登录哟 !",
        icon: "none"
      });
    }
  }
});
